package b2.scene.guide
{
	import b2.net.*;
	import b2.scene.*;
	import b2.scene.base.*;
	import b2.scene.camp.*;
	import b2.scene.fight.PlayState;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tank.*;
	import b2.widget.fort.*;
	import b2.widget.guide.*;
	import b2.widget.tab.*;
	import b2.widget.warehouse.*;
	
	import flash.display.*;
	import flash.geom.Point;
	
	import org.flixel.*;

	public class GuideScene extends BasicScene
	{
		static private const ALICE_NAME:String = "<font color='#009000'>李华梅</font> <font color='#0000FF'>参谋</font>";
		static private var instance:GuideScene;

		private var _step:int;
		private var container:Sprite;
		private var widgetOldPosX:Number;
		private var widgetOldPosY:Number;
		private var widget:Sprite;
		private var widgetParent:DisplayObjectContainer;
		
		public function GuideScene()
		{
			super();
		}
		
		static public function getInstance():GuideScene
		{
			if (!instance)
				instance = new GuideScene();
			return instance;
		}
		
		public function get enabled():Boolean
		{
			return (this.parent != null)
		}
		
		public function clearLastStep():void
		{
			if (container != null && container.parent != null)
			{
				container.parent.removeChild(container);
				container = null;
			}
			if (widget != null)
			{
				widget.x = this.widgetOldPosX;
				widget.y = this.widgetOldPosY;
				widgetParent.addChild(widget);
				widget = null;
			}
		}
		
		public function get step():int
		{
			return _step;
		}

		public function set step(s:int):void
		{
			if (!enabled)
				return;
			clearLastStep();
			container = new Sprite();
			addChild(container);
			_step = s;
			switch (s)
			{
				case 0:
					selectTank();
					break;
				case 1:
					showWelcomeMessage();
					break;
				case 2:
					gotoCamp();
					break;
				case 3:
					selectFort();
					break;
				case 4:
					startGame();
					break;
				case 5:
					step = 6;
					break;
				case 6:
					useWtoMove();
					break;
				case 7:
					useDtoMove();
					break;
				case 8:
					continueToMove();
					break;
				case 9:
					startFire();
					break;
				case 10:
					fetchItem();
					break;
				case 11:
					finishFight();
					break;
				case 12:
					startUseWeapon();
					break;
				case 13:
					doubleClickWeapon();
					break;
				case 14:
					onWeaponEquiped();
					break;
				case 15:
					selectFortAgain();
					break;
				case 16:
					startGame();
					break;
				case 17:
					step = 18;
					break;
				case 18:
					useWeaponToFire();
					break;
				case 19:
					showGuideComplete();
					break;
				case 20:
					finishGuide();
					break;
			}
		}
		
		private function selectTank():void
		{
			addBackground();
			var tankSelectDialog:TankSelectDialog = new TankSelectDialog();
			tankSelectDialog.x = (BFont.stageWidth - tankSelectDialog.width) / 2;
			tankSelectDialog.y = (BFont.stageHeight - tankSelectDialog.height) / 2;
			container.addChild(tankSelectDialog);
		}
		
		private function showWelcomeMessage():void
		{
			addBackground();
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "欢迎你，新兵！\n我代表军部欢迎你的到来。", true);
			guideMessageBox.x = (BFont.stageWidth - guideMessageBox.width) / 2;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
		}
		
		private function gotoCamp():void
		{
			addBackground();
			addClickableWidget(BaseScene.getInstance().campIcon);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "我们马上去战场报到吧！\n");
			guideMessageBox.x = 330;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
			var guideArrow:GuideArrow = new GuideArrow(1, "请点击出战", 100, 80);
			guideArrow.x = 153;
			guideArrow.y = 380;
			container.addChild(guideArrow);
		}
		
		private function selectFort():void
		{
			addBackground();
			addClickableWidget(CampScene.getInstance().fortressList[2]);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "进入<font color='#FF0000'> 灰狼要塞 </font>，你可以学习到基本的坦克驾驶和开炮技巧。\n");
			guideMessageBox.x = 400;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
			var guideArrow:GuideArrow = new GuideArrow(0, "请点击进入灰狼要塞", 110, 80);
			guideArrow.x = 215;
			guideArrow.y = 340;
			container.addChild(guideArrow);
		}
		
		private function startGame():void
		{
			addBackground();
			var guideArrow:GuideArrow = new GuideArrow(1, "请点击开始游戏", 110, 80);
			guideArrow.x = 440;
			guideArrow.y = 344;
			container.addChild(guideArrow);
		}
		
		private function useWtoMove():void
		{
			var playstate:PlayState = FlxG.state as PlayState;
			playstate.addDestFlag(4, 4);
			addBackground(0x000000, 0.01);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "请<font color='#FF0000'> 点击鼠标 </font>将坦克移动到指定位置。", false, false, 0.2);
			guideMessageBox.x = 400;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
		}
		
		private function useDtoMove():void
		{
			var playstate:PlayState = FlxG.state as PlayState;
			playstate.removeDestFlag();
			playstate.addDestFlag(9, 4);
			addBackground(0x000000, 0.01);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "请<font color='#FF0000'> 点击鼠标 </font>将坦克移动到指定位置。", false, false, 0.2);
			guideMessageBox.x = 400;
			guideMessageBox.y = 300;
			container.addChild(guideMessageBox);
		}
		
		private function continueToMove():void
		{
			var playstate:PlayState = FlxG.state as PlayState;
			playstate.removeDestFlag();
			playstate.addDestFlag(5, 8);
			addBackground(0x000000, 0.01);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "请<font color='#FF0000'> 点击鼠标 </font>将坦克移动到指定位置。", false, false, 0.2);
			guideMessageBox.x = 400;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
		}
		
		private function startFire():void
		{
			var playstate:PlayState = FlxG.state as PlayState;
			playstate.removeDestFlag();
			addBackground(0x000000, 0.01);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "请<font color='#FF0000'> 点击鼠标 </font>攻击教练车，你的坦克会自动发炮攻击目标哦。", false, false, 0.2);
			guideMessageBox.x = 400;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
		}
		
		private function fetchItem():void
		{
			addBackground(0x000000, 0.01);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "请移动到<font color='#FF0000'> 宝箱 </font>的位置以捡起宝箱。", false, false, 0.2);
			guideMessageBox.x = 400;
			guideMessageBox.y = 300;
			container.addChild(guideMessageBox);
		}
		
		private function finishFight():void
		{
			
		}
		
		private function startUseWeapon():void
		{
			var widget:Sprite = WidgetManager.getInstance().getWidget(FortressDialog) as Sprite;
			if (widget != null && widget.parent != null)
				widget.parent.removeChild(widget);
			addBackground();
			addClickableWidget(CampScene.getInstance().bottomBar.warehouseIcon);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "恭喜你获得了<font color='#FF0000'> 绝对零度 </font>，让我们来装备武器吧。");
			guideMessageBox.x = 330;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
			var guideArrow:GuideArrow = new GuideArrow(1, "请点击仓库开始装备", 100, 80);
			guideArrow.x = 240;
			guideArrow.y = 380;
			container.addChild(guideArrow);
		}
		
		private function doubleClickWeapon():void
		{
			addBackground();
			var tab:Tab = Warehouse.getInstance().tabs[1] as Tab;
			var objectItem:ObjectItem;
			for (var i:int = 0; i < tab.items.length; i++)
			{
				objectItem = tab.items[i]; 
				if (objectItem.objectIndex == 20006)
				{
					addClickableWidget(objectItem);
					break;
				}
			}
			var globalPoint:Point = widget.parent.localToGlobal(new Point(objectItem.x, objectItem.y));
			var guideArrow:GuideArrow = new GuideArrow(0, "请双击装备物品", 100, 80);
			guideArrow.x = globalPoint.x - 15;
			guideArrow.y = 295;
			container.addChild(guideArrow);
		}
		
		private function onWeaponEquiped():void
		{
			addBackground();
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "<font color='#FF0000'> 绝对零度 </font>已经成功装备到了你的坦克上，去实战中试试新武器的威力吧！", true);
			guideMessageBox.x = 400;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
		}
		
		private function selectFortAgain():void
		{
			var widget:Sprite = WidgetManager.getInstance().getWidget(Warehouse) as Sprite;
			if (widget != null && widget.parent != null)
				widget.parent.removeChild(widget);
			widget = WidgetManager.getInstance().getWidget(TankDialog) as Sprite;
			if (widget.parent != null)
				widget.parent.removeChild(widget);
			addBackground();
			addClickableWidget(CampScene.getInstance().fortressList[2]);
			var guideArrow:GuideArrow = new GuideArrow(0, "还是刚才这里，请点击进入。", 110, 80);
			guideArrow.x = 215;
			guideArrow.y = 340;
			container.addChild(guideArrow);
		}
		
		private function useWeaponToFire():void
		{
			addBackground(0x000000, 0.01);
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "再次<font color='#FF0000'> 点击鼠标 </font>攻击教练车试试，新的武器会自动发挥作用哦！", false, false, 0.2);
			guideMessageBox.x = 400;
			guideMessageBox.y = 300;
			container.addChild(guideMessageBox);
		}
		
		private function showGuideComplete():void
		{
			var widget:Sprite = WidgetManager.getInstance().getWidget(FortressDialog) as Sprite;
			if (widget != null && widget.parent != null)
				widget.parent.removeChild(widget);
			addBackground();
			var guideMessageBox:GuideMessageBox = new GuideMessageBox(ALICE_NAME, "恭喜你完成了新手引导！\n这就开始你的坦克征途吧。", false, true);
			guideMessageBox.x = 240;
			guideMessageBox.y = 200;
			container.addChild(guideMessageBox);
		}
		
		private function finishGuide():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
			SceneManager.getInstance().loadScene(BaseScene.getInstance());
			ServerHelper.getInstance().finishGuide();
			ServerHelper.getInstance().addEventListener(ServerEvent.FINISH_GUIDE, onFinishGuide);
		}
		
		private function onFinishGuide(event:ServerEvent):void
		{
			BottomBar.showTaskDialog(true);
		}
		
		private function addBackground(color:uint = 0x000000, alpha:Number = 0.5):void
		{
			var bgr:Sprite = new Sprite();
			var gfx:Graphics = bgr.graphics;
			gfx.moveTo(0, 0);
			gfx.beginFill(color, alpha);
			gfx.lineTo(BFont.stageWidth, 0);
			gfx.lineTo(BFont.stageWidth, BFont.stageHeight);
			gfx.lineTo(0, BFont.stageHeight);
			gfx.lineTo(0, 0);
			gfx.endFill();
			container.addChild(bgr);
		}
		
		public function addClickableWidget(widget:Sprite):void
		{
			if (!enabled)
				return;
			this.widgetOldPosX = widget.x;
			this.widgetOldPosY = widget.y;
			this.widget = widget;
			this.widgetParent = widget.parent;
			var globalPoint:Point = widget.parent.localToGlobal(new Point(widget.x, widget.y));
			widget.x = globalPoint.x;
			widget.y = globalPoint.y;
			container.addChild(widget);
		}
	}
}